<?php

namespace Home\Controller;
use Think\Controller;
//http://127.0.0.1/case/Home/Index/index
class GupiaoController extends CommonController{

	//取得2100一下的所有深圳股票
    function get_all_stock() {
		$item = null;
    	for($i = 1;$i<=2700;$i++){
			$strlen = strlen($i);
			$num0 = 6-$strlen;

			$strlen0 = null;
			for($j=0;$j<$num0;$j++){
				$strlen0.='0';
			}
			$code = $strlen0.$i;

	    	$url = 'http://hq.sinajs.cn/list=sz'.$code;
	    	$str = file_get_contents($url);
	    	$str = mb_convert_encoding($str, "UTF-8", "GBK");

			$data = explode(',',$str);
			$str_name = explode('"',$data[0]);
			if(!empty($str_name[1])){
				$item[$i-1]['code'] = $code;
				$item[$i-1]['name'] = $str_name[1];
				$item[$i-1]['now_price'] =$data[1];
			}
    	}

    	foreach($item as $k=>$l){
			$m = M('gupiao');
			$map['code'] = $l['code'];
			$map['name'] = $l['name'];
			$num = $m->where($map)->count();
			if($num == 0){
				$map['now_price']=$l['now_price'];
				$m->add($map);
				echo $m->getlastsql()."<br>";
				echo "The End";
			}
    	}
    }


	function get_equity(){
		$m = M('gupiao');
		$list = $m->select();

		foreach($list as $k=>$l){
//			if($k  == 0){
				$url = 'http://quote.eastmoney.com/sz'.$l['code'].'.html';
				$content = file_get_contents($url);
				$content = mb_convert_encoding($content, "UTF-8", "GBK");
				$content = explode("总股本：",$content);
				$content = explode("亿",$content[1]);
				$equity = $content[0];

				$l['total_equity'] = $equity;
				$m->save($l);
				echo $m->getlastsql()."<br>";
//			}
		}
	}



	function get_filter(){
		$m = M('gupiao');
		$filter = M('gupiao_filter');
		$list = $m->select();
		foreach($list as $k=>$l){
			if(strpos($l['total_equity'],"万") !== false){
				echo $l['code'].'==='.$l['name']."===".$l['total_equity']."<br>";
				print_r(explode('万',$l['total_equity']));
				$total_equity = explode('万',$l['total_equity']);
				$total_equity = intval($total_equity[0])/10000;

				$l['total_equity'] = $total_equity;
				$filter->add($l);
			}else{
//				echo $l['code'].'==='.$l['name']."===".$l['total_equity']."<br>";
				$filter->add($l);
			}
		}





	}







}
?>